import {createSlice} from "@reduxjs/toolkit";
import {
    getProfessionalCategoryCountAPI,
    getProfessionalCategoryCountByNameAPI,
    getProfessionalCategoryInfoAPI,
    getProfessionalCategoryListAPI,
    getProfessionalCategoryListByNameAPI
} from "@/apis/ProfessionalCategory.jsx";

const ProfessionalCategoryStore=createSlice({
    name:'professionalCategory',
    initialState:{
        professionalCategoryInfo_init:{},
        professionalCategoryList_init:[],
        professionalCategoryCount_init:0,
        professionalCategoryCur_init:1,
        professionalCategoryPageSize_init:11,
        professionalCategoryDriveListChange_init:0
    },
    reducers:{
        getProfessionalCategoryInfo(state,action){
            state.professionalCategoryInfo_init=action.payload
        },
        getProfessionalCategoryList(state,action){
            state.professionalCategoryList_init=action.payload
        },
        getProfessionalCategoryCount(state,action){
            state.professionalCategoryCount_init=action.payload
        },
        setProfessionalCategoryCur(state,action){
            state.professionalCategoryCur_init=action.payload;
        },
        setProfessionalCategoryPageSize(state,action){
            state.professionalCategoryPageSize_init=action.payload;
        },
        setProfessionalCategoryDriveListChange(state,action){
            state.professionalCategoryDriveListChange_init=action.payload;
        }
    }
})
const {
    getProfessionalCategoryInfo,
    getProfessionalCategoryList,
    getProfessionalCategoryCount,
    setProfessionalCategoryCur,
    setProfessionalCategoryPageSize,
    setProfessionalCategoryDriveListChange}=ProfessionalCategoryStore.actions;

// 异步请求函数
// 分页获取所有数据
const fetchGetProfessionalCategoryList=(cur,pageSize)=>{
    return async (dispatch)=>{
        const res=await getProfessionalCategoryListAPI(cur,pageSize);
        dispatch(getProfessionalCategoryList(res.data))
    }
}
const fetchGetProfessionalCategoryInfo=(id)=>{
    return async (dispatch)=>{
        const res=await getProfessionalCategoryInfoAPI(id);
        dispatch(getProfessionalCategoryInfo(res.data))
    }
}
const fetchGetProfessionalCategoryCount=()=>{
    return async (dispatch)=>{
        const res=await getProfessionalCategoryCountAPI();
        dispatch(getProfessionalCategoryCount(res.data))
    }
}

// 根据项目名称查询项目
const fetchGetProfessionalCategoryListByName=(name,cur,pageSize)=>{
    return async (dispatch)=>{
        const res=await getProfessionalCategoryListByNameAPI(name,cur,pageSize);
        console.log(res.data);
        dispatch(getProfessionalCategoryList(res.data));

    }
}
const fetchGetProfessionalCategoryCountByName=(name)=>{
    return async (dispatch)=>{
        const res=await getProfessionalCategoryCountByNameAPI(name);
        console.log(res.data);
        dispatch(getProfessionalCategoryCount(res.data))
    }
}

const professionalCategoryReducer=ProfessionalCategoryStore.reducer;
export {
    fetchGetProfessionalCategoryList,
    fetchGetProfessionalCategoryInfo,
    fetchGetProfessionalCategoryCount,
    fetchGetProfessionalCategoryListByName,
    fetchGetProfessionalCategoryCountByName,
    setProfessionalCategoryCur,
    setProfessionalCategoryPageSize,
    setProfessionalCategoryDriveListChange
}

export default professionalCategoryReducer;